package com.ricchieri.manage.bean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import com.ricchieri.common.BusinessSettings;
import com.ricchieri.filter.CharterFilter;
import com.ricchieri.lazymodel.CharterLazyDataModel;
import com.ricchieri.model.Charter;
import com.ricchieri.model.CharterType;
import com.ricchieri.service.interf.ICharterService;
import com.ricchieri.service.interf.ICharterTypeService;
import com.ricchieri.util.FacesUtil;
import com.ricchieri.util.StringUtils;

@ManagedBean(name = "searchCharterMB")
@ViewScoped
public class SearchCharterManageBean extends GenericManageBean {

	private static final long serialVersionUID = -275710803306101330L;
	
	@ManagedProperty(value = "#{CharterService}")
	ICharterService charterService;
	
	@ManagedProperty(value = "#{CharterTypeService}")
	ICharterTypeService charterTypeService;

	private Map<String,String> charterTypes = null;
	private CharterFilter charterFilter;
	private Charter selectedCharter;
	private String charterNumber;
	
	private CharterLazyDataModel charterLazyDataModel;
	
	
	@PostConstruct
	public void init() {
		charterFilter =  (CharterFilter) FacesUtil.getParametersFlash(BusinessSettings.KEY_FILTER);
		
		if(charterFilter == null) {
			charterFilter = new CharterFilter();
		}else{
			charterNumber = String.valueOf(charterFilter.getCharterNumber());
		}
	}
	
	public Map<String, String> getCharterTypes() {
		if (charterTypes==null){
			List<CharterType> charterT = getCharterTypeService().getAllCharterTypes();		
			
			charterTypes = new HashMap<String, String>();
			
			for (CharterType charterType : charterT) {
				getCharterTypes().put(FacesUtil.getMessage(charterType.getDescription()), String.valueOf(charterType.getId()));
			}
		}
		
		return charterTypes;
	}
	
	public void search() {
		if (!StringUtils.isEmpty(charterNumber)) {
			charterFilter.setCharterNumber(Integer.parseInt(charterNumber));
		}else{
			charterFilter.setCharterNumber(0);
		}
		charterLazyDataModel = null;
	}
	
	
	public String editCharter(Charter charter) {
		FacesUtil.addParametersFlash(BusinessSettings.KEY_CHARTER, charter);
		FacesUtil.addParametersFlash(BusinessSettings.KEY_FILTER, charterFilter);
		
		return BusinessSettings.EDIT;
	}
	
	public void reset() {
		charterFilter = new CharterFilter();
		charterNumber = "";
		charterLazyDataModel = null;
	}

	public String viewCharter(Charter charter) {
		FacesUtil.addParametersFlash(BusinessSettings.KEY_CHARTER, charter);
		FacesUtil.addParametersFlash(BusinessSettings.KEY_FILTER, charterFilter);
		return BusinessSettings.VIEW_DETAILS;
	}
	
	public String addCharterExpense(Charter charter) {
		FacesUtil.addParametersFlash(BusinessSettings.KEY_CHARTER, charter);
		FacesUtil.addParametersFlash(BusinessSettings.KEY_FILTER, charterFilter);
		
		return BusinessSettings.ADD;
	}

	
	public void showDialog(Charter charter) {
		selectedCharter = charter;
	}

	public Charter getSelectedCharter() {
		return selectedCharter;
	}

	public void setSelectedCharter(Charter selectedCharter) {
		this.selectedCharter = selectedCharter;
	}
	
	public void delete() {
		getCharterLazyDataModel().setRowCount(getCharterLazyDataModel().getRowCount() -1 );
		getCharterService().deleteCharter(getSelectedCharter());
		FacesUtil.showInfoMessage("searchCharterForm", "charter_deleted");
	}
	
	public void unselect() {
		this.setSelectedCharter(null);
	}

	public ICharterService getCharterService() {
		return charterService;
	}

	public void setCharterService(ICharterService charterService) {
		this.charterService = charterService;
	}

	public CharterFilter getCharterFilter() {
		return charterFilter;
	}

	public void setCharterFilter(CharterFilter charterFilter) {
		this.charterFilter = charterFilter;
	}

	public ICharterTypeService getCharterTypeService() {
		return charterTypeService;
	}

	public void setCharterTypeService(ICharterTypeService charterTypeService) {
		this.charterTypeService = charterTypeService;
	}

	public void setCharterTypes(Map<String, String> charterTypes) {
		this.charterTypes = charterTypes;
	}

	public String getCharterNumber() {
		return charterNumber;
	}

	public void setCharterNumber(String charterNumber) {
		this.charterNumber = charterNumber;
	}

	public CharterLazyDataModel getCharterLazyDataModel() {
		if(charterLazyDataModel  == null) {
			charterLazyDataModel = new CharterLazyDataModel(charterService, charterFilter);
		}
		return charterLazyDataModel;
	}

	public void setCharterLazyDataModel(CharterLazyDataModel charterLazyDataModel) {
		this.charterLazyDataModel = charterLazyDataModel;
	}
}

